<?php
namespace app\index\relevan;
use app\index\controller\Common;
use app\index\model\ContractBianListModel;
use app\index\model\ContractBianModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractCaiListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractShiModel;
use app\index\model\EnginBudbianListModel;
use app\index\model\EnginBudbianModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginFoldModel;
use app\index\model\EnginModel;
use app\index\model\StuffChuModel;
use app\index\model\StuffRuListModel;
use app\index\model\StuffRuModel;
use app\index\service\StuffService;

class EnginRelevan extends Common
{

    /**
     * 创建项目 动态生成相关数据
     * @param $enginid
     * @return bool
     */
    public function vate_engin_add_mingxi($enginid){
        //生成项目默认文件资料
        $fold = [
            '施工组织方案', '施工日志', '技术交底', '设计变更', '会议纪要', '工程联络单', '收发文',
            '材料合格证', '试验资料', '验收资料', '竣工资料',
        ];
        foreach ($fold as $v){
            EnginFoldModel::insert([
                'comid' => $this->userinfo['comid'],
                'pid' => 0,
                'enginid' => $enginid,
                'type' => 0,
                'name' => $v,
                'created_user' => 0,
                'created_at' => date('Y-m-d H:i:s'),
            ]);
        }

        return true;
    }


    /**
     * 更新项目管理组
     * @param $enginid
     * @return bool
     */
    public function vate_engin($minfo){
        $upd = [];
        if($minfo['business'] == 1){
            $upd['man_zhuan'] = $this->cominfo['man_zhuan'];
            $upd['man_zi'] = $this->cominfo['man_zi'];
            $upd['man_cheng'] = $this->cominfo['man_cheng'];
            $upd['man_zhian'] = $this->cominfo['man_zhian'];
        }else{
            $upd['man_zhuan'] = $this->cominfo['man_zhuan_lian'];
            $upd['man_zi'] = $this->cominfo['man_zi_lian'];
            $upd['man_cheng'] = $this->cominfo['man_cheng_lian'];
            $upd['man_zhian'] = $this->cominfo['man_zhian_lian'];
        }
        EnginModel::where(['id'=>$minfo['id']])->update($upd);
        return true;
    }



    /**
     * 更新项目竣工日期
     * @param $data
     * @return bool
     */
    public function vate_engin_jundt($data){
        EnginModel::where(['id'=>$data['enginid']])->update(['actualdt'=>$data['actualdt'],'filishdt'=>$data['filishdt']]);
        return true;
    }

    /**
     * 更新项目施工合同金额
     * 同步更新  施工合同的更新后金额   【包含补充合同】 (审核中 + 审核完成)
     * @param $enginid
     * @return bool
     */
    public function vate_engin_shimoney($enginid){
        $shimoney = 0;
        $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$enginid,'deleted_user'=>0,'isturn'=>1])
            ->where('status','in',[0,1])->find();
        if($shiinfo){
            $shimoney += $shiinfo['money'];
            $shimoney += ContractBuModel::where(['shiid'=>$shiinfo['id'],'deleted_user'=>0,'isturn'=>1])
                ->where('status','in',[0,1])->sum('money');
            ContractShiModel::where(['id'=>$shiinfo['id']])->update(['shimoney'=>$shimoney]);
        }
        EnginModel::where(['id'=>$enginid])->update(['shimoney'=>$shimoney]);
        return true;
    }



    /**
     * 更新项目成本预算【变更后】
     * 同步更新  成本预算的更新后金额
     * @param $bianid
     * @return bool
     */
    public function vate_engin_budget_list($bianid){
        $bianInfo = EnginBudbianModel::where(['id'=>$bianid])->find();
        if(!$bianInfo){
            return true;
        }

        $budgetInfo = EnginBudgetModel::where(['id'=>$bianInfo['budgetid']])->find();
        if(!$budgetInfo){
            return true;
        }

        $bianlist = EnginBudbianListModel::where(['budid'=>$bianInfo['id']])->select()->toArray();

        foreach ($bianlist as $vv){
            if($vv['listid'] > 0){
                EnginBudgetListModel::where(['id'=>$vv['listid']])->update([
                    'num' => $vv['new_num'],
                    'price' => $vv['new_price'],
                    'money' => $vv['new_money'],
                ]);
            }else{
                EnginBudgetListModel::insert([
                    'budid' => $bianInfo['budgetid'],
                    'subid' => $vv['subid'],
                    'name' => $vv['name'],
                    'unit' => $vv['unit'],
                    'num' => $vv['num'],
                    'price' => $vv['price'],
                    'money' => $vv['money'],
                    'explain' => $vv['explain'],
                ]);
            }
        }

        $listMoney = EnginBudgetListModel::where(['budid'=>$bianInfo['budgetid']])->sum('money');
        EnginBudgetModel::where(['id'=>$bianInfo['budgetid']])->update(['money'=>$listMoney]);
        return true;
    }



    /**
     * 更新采购合同变更明细 及其 变更后金额
     * @param $bianid
     * @return bool
     */
    public function vate_contract_cai_list($bianid){
        $bianinfo = ContractBianModel::where(['id'=>$bianid])->find();

        $cai_money = ContractCaiModel::where(['id'=>$bianinfo['caiid']])->value('money');
        $bian_money = $cai_money + $bianinfo['bianfee'];
        ContractBianModel::where(['id'=>$bianinfo['id']])->update(['yuanfee'=>$cai_money]);
        ContractCaiModel::where(['id'=>$bianinfo['caiid']])->update(['money'=>$bian_money]);


        $list = ContractBianListModel::where(['bianid'=>$bianinfo['id']])->select()->toArray();
        foreach($list as $vv){
            if($vv['listid'] > 0){
                ContractCaiListModel::where(['id'=>$vv['listid']])->update([
                    'listid' => $vv['subid'],
                    'num' => $vv['num'],
                    'price' => $vv['price'],
                    'money' => $vv['money'],
                ]);
            }else{
                ContractCaiListModel::insert([
                    'caiid' => $bianinfo['caiid'],
                    'listid' => $vv['subid'],
                    'name' => $vv['name'],
                    'unit' => $vv['unit'],
                    'num' => $vv['num'],
                    'price' => $vv['price'],
                    'money' => $vv['money'],
                    'explain' => $vv['explain'],
                ]);
            }
        }
        return true;
    }


    /**
     * 材料入库 生成出库单
     * @param $bianid
     * @return bool
     */
    public function vate_stuff_ru_create_chu($minfo){
        if($minfo['ischu'] > 0){
            $list = StuffRuListModel::where(['ruid'=>$minfo['id']])->select()->toArray();
            foreach ($list as $vvv){
                $ins = [];
                $ins['comid'] = $this->userinfo['comid'];
                $ins['chanid'] = $minfo['chanid'];
                $ins['deptid'] = $minfo['deptid'];
                $ins['enginid'] = $minfo['enginid'];
                $ins['ruid'] = $vvv['id'];
                $ins['number'] = (new StuffService())->create_stuff_chu_number();
                $ins['chudt'] = $minfo['rudt'];
                $ins['baner'] = $minfo['baner_chu'];
                $ins['num'] = $vvv['num'];
                $ins['created_user'] = $this->userinfo['id'];
                $ins['created_at'] = date("Y-m-d H:i:s");
                StuffChuModel::insert($ins);
            }
        }
        return true;
    }



}
